<?php
define('LEGAL_OPER', true);
require('../../includes/admin-init.php');
require(ADMIN_PATH . '/class/CommonData.class.php');

//列表页
if($method == 'list'){
	$db= new DB();
	$categorys = CommonData::getCategorys($db->getDb());
	$smarty->assign("categorys",$categorys);
	$smarty->display("goods/category_list.htm");
	exit;
}


//列表页
if($method == 'addui'){
	$pid = to_int($_GET['pid']);
	if($pid > 0){
		$db= new DB();
		$catTable = new BaseDao($db->getDb(),'goods_category','cat_id');
		$parent = $catTable->getById($pid);
		$smarty->assign("parent",$parent);
	}
	$smarty->display("goods/category_add.htm");
	exit;
}

if($method == 'add'){
	$pid = to_int($_POST['pid']);
	$name = isset($_POST['name'])?trim($_POST['name']):'';
	$unit = isset($_POST['unit'])?trim($_POST['unit']):'';
	$sort =  to_int($_POST['sortorder']);
	$pid = $pid > 0?$pid:0;
	$sort = $sort > 0?$sort:1;
	
	if($name != ''){
		$db= new DB();
		$db->beginTransaction();
		$catTable = new BaseDao($db->getDb(),'goods_category','cat_id');
		$sets = array();
		$sets['cat_name'] = $name;
		$sets['parent_id'] = $pid;
		$sets['cat_unit'] = $unit;
		$sets['sort_order'] = $sort;
		try{
			$catTable->save($sets);
			$db->commit();
			$url = '/sys-admin/action/goods/category.php?met=addsuccess';
			to_redirect($url);
			
		}catch(Exception $e){
			$db->rollback();
			$url = '/sys-admin/action/goods/category.php?met=addfail';
			to_redirect($url);
		}
		$db->close();
	}else{
		$links[0]['text'] = '分类列表';
		$links[0]['href'] = '/sys-admin/action/goods/category.php?met=list';
		to_message($smarty, "数据错误",1,$links);
	}
	exit;
}

if($method == 'addsuccess'){
	$links[0]['text'] = '分类列表';
	$links[0]['href'] = '/sys-admin/action/goods/category.php?met=list';
	to_message($smarty, "分类添加成功",0,$links);
}
if($method == 'addfail'){
	$links[0]['text'] = '分类列表';
	$links[0]['href'] = '/sys-admin/action/goods/category.php?met=list';
	to_message($smarty, "分类添加失败",1,$links);
}


//修改页
if($method == 'editui'){
	$id = to_int($_GET['id']);
	$db= new DB();
	$catTable = new BaseDao($db->getDb(),'goods_category','cat_id');
	$category = $catTable->getById($id);
	$categorys = CommonData::getCategorys($db->getDb());
	$db->close();
	$smarty->assign("category",$category);
	$smarty->assign("categorys",$categorys);
	$smarty->display("goods/category_edit.htm");
	exit;
}

//修改
if($method == 'edit'){
	$id = to_int($_POST['id']);
	$name = isset($_POST['name'])?trim($_POST['name']):'';
	$unit = isset($_POST['unit'])?trim($_POST['unit']):'';
	$sort =  to_int($_POST['sortorder']);
	$sort = $sort > 0?$sort:1;
	$isshow =  isset($_POST['isshow'])?(trim($_POST['isshow'])=='0'?0:1):1;
	$isshownav =  isset($_POST['isshownav'])?(trim($_POST['isshownav'])=='0'?0:1):1;
	$parentid = to_int($_POST['parentid']);
	$parentid = $parentid>0?$parentid:0;
	
	if($name != '' && $id > 0){
		$db= new DB();
		$db->beginTransaction();
		$catTable = new BaseDao($db->getDb(),'goods_category','cat_id');
		$sets = array();
		$sets['cat_name'] = $name;
		$sets['cat_unit'] = $unit;
		$sets['sort_order'] = $sort;
		$sets['is_show_nav'] = $isshownav;
		$sets['is_show'] = $isshow;
		$sets['parent_id'] = $parentid;
		
		try{
			$catTable->update($sets, 'cat_id=?', array($id));
			$db->commit();
			$url = '/sys-admin/action/goods/category.php?met=editsuccess';
			to_redirect($url);
				
		}catch(Exception $e){
			$db->rollback();
			$url = '/sys-admin/action/goods/category.php?met=editfail';
			to_redirect($url);
		}
		$db->close();
	}else{
		$links[0]['text'] = '分类列表';
		$links[0]['href'] = '/sys-admin/action/goods/category.php?met=list';
		to_message($smarty, "数据错误",1,$links);
	}
}
if($method == 'editsuccess'){
	$links[0]['text'] = '分类列表';
	$links[0]['href'] = '/sys-admin/action/goods/category.php?met=list';
	to_message($smarty, "分类修改成功",0,$links);
}
if($method == 'editfail'){
	$links[0]['text'] = '分类列表';
	$links[0]['href'] = '/sys-admin/action/goods/category.php?met=list';
	to_message($smarty, "分类修改失败",1,$links);
}

//删除
if($method == 'delete'){
	$id = to_int($_GET['id']);
	$db = new DB();
	$db->beginTransaction();
	$base = new BaseDao($db->getDb(),'goods_category','cat_id');
	try{
		$count = $base->getCount('parent_id=?', array($id));
		if($count > 0){
			echo '2';
		}else{
			$base->delete('cat_id=?', array($id));
			$db->commit();
			echo '1';
		}
	}catch(Exception $e){
		$db->rollback();
		echo '0';
	}
	$db->close();
	exit;
}

